/*
 * Copyright (C) 2010 Peter Schönhofen
 *
 * This file is part of the Akkad Compiler Framework.
 *
 * The Akkad Compiler Framework is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */

package hu.anaxo.akkad

import hu.anaxo.akkad.processor.pl0.PL0
import hu.anaxo.akkad.vm.Executor
import io.Source
import java.io.FileNotFoundException

/**
 * Birch Compiler Framework Release 1
 */

object Akkad {

  // languages

  private val languages = Map("pl0" -> PL0)

  /**
   * Main method
   *
   * @param args command line arguments
   */

  def main(args: Array[String]): Unit = {

    // display greeting

    println("Akkad Compiler Framework Release 0.1")
    println("Copyright (C) 2010 Peter Schönhofen")
    println

    // actual processing

    try {

      // preparations

      if (args.length != 2)
        throw new Exception("Arguments should be: <language> <source>")
      if (!languages.contains(args(0)))
        throw new Exception("Unknown language " + args(0))
      val language = languages(args(0))
      val source = Source.fromPath(args(1)).mkString

      // perform translation

      val program = language.process(source)

      // execute program

      Executor.execute(program)

    // handle exceptions

    } catch {
      case exception: FileNotFoundException =>
        println("Cannot open source file")
      case exception: Exception =>
        println("Internal error")
        exception.printStackTrace
    }
  }
}